var zuhePage = (function ($) {
	var perId = '' //内分泌的刘亭婷 
	var wardID = ''
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function getGroupList(){
		var pdict = {"start":0,'limit':"",'WardID':wardID}
		var paramDict = {'method':'web.INMDBComm:FindWardPostGroupList','params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1' || result['data'].length == 0) {
				showNullTip(result['msg'])
				mui('#tbody')[0].innerHTML = ''
				return
			}
			hideNullTip()
			updateGroupUI(result['data'])
		}, function(errorStr) {
			showNullTip(errorStr,'error')
			$('#tbody')[0].innerHTML = ''
		});
	}
	function updateGroupUI(dataArr){
		var html = ''
		for (var i = 0; i < dataArr.length; i++) {
			var oneData = dataArr[i]
			html +='<tr id="tr-'+i+'" data-id="'+oneData['rw']+'"><td class="name">'+oneData['GroupDesc']+'</td><td class="content">'+
				'<div id="inner-'+i+'" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">'+
				'<div class="mui-scroll">';
			
			var postArr = oneData['GroupPostDesc'].split(',')
			for (var j = 0; j < postArr.length; j++) {
				html +='<div class="mui-control-item mui-active">'+postArr[j]+'</div>'
			}
			html +='</div></div></td><td class="editBtns">'+
				'<div class="mui-icon mui-icon-compose"></div>'+ //编辑
				'<div class="mui-icon mui-icon-trash"></div>'+	//删除
				//'<div class="mui-icon mui-icon-arrowthinup"></div>'+ //向上
				//'<div class="mui-icon mui-icon-bars"></div>'+  //拖拽
			'</td></tr>'
		}
		$('#tbody')[0].innerHTML = html
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		mui('#tbody .name').each(function (index,element) {
			mui('#inner-'+index).scroll().scrollTo(0,0,0);
		})
		if (html == '') {
			showNullTip('暂无数据')
		}
	}
	function deleteOneGroup(rw){
		var pdict = {"rw":rw}
		var paramDict = {'method':'web.INMDBComm:DeleteWardPostGroup','params':JSON.stringify(pdict),'type':'Method'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			console.log(JSON.stringify(result))
			if (result['msg'] != '1') {
				mui.toast('删除失败')
				return
			}
			getGroupList()
		}, function(errorStr) {
			mui.toast('删除失败')
		});
	}
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview();
		sourceData = self.sourceData
		perId=sourceData['nurseid']
		wardID = sourceData['wardID']
		//编辑
		mui('#tbody').on('tap','.mui-icon-compose',function(){
			var tr = this.parentNode.parentNode
			var rw = tr.getAttribute('data-id')
			var webview = plus.webview.create('nurSchZuHeAdd.html', 'nurSchZuHeAdd', {
				background: '#FF0000',
				statusbar: {
					background: '#00FF00'
				}
			},{rw:rw,nurseid:perId,wardID:wardID});
			webview.show('fade-in');
		})

		//删除
		mui('#tbody').on('tap','.mui-icon-trash',function(){
			console.log('删除')
			var tr = this.parentNode.parentNode
			var rw = tr.getAttribute('data-id')
			mui.confirm('确定删除这个组合吗','温馨提示',['取消','确认'],function (e) {
				if (e.index == 1) {
					deleteOneGroup(rw)
				}
			})
		});
		//新增组合按钮
		$('#addBtn')[0].addEventListener('tap',function(){
			var webview = plus.webview.create('nurSchZuHeAdd.html', 'nurSchZuHeAdd', {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'nurseid':perId,'wardID':wardID});
			webview.show('fade-in');
		})
		//编辑排序按钮
		$('#editBtn')[0].addEventListener('tap',function(){
			document.body.classList.add('editing')
		})
		
		mui.back = function(){
			var classStr = document.body.getAttribute('class')
			if (classStr && classStr.indexOf('editing') != -1) {
				document.body.classList.remove('editing')
				return
			}
			var nurSchLoc = plus.webview.getWebviewById('nurSchLoc')
			if (nurSchLoc) {
				nurSchLoc.evalJS('refreshPageData()')
			}
			plus.webview.currentWebview().close()
		}
		getGroupList()
	}
	function showNullTip(msg,type){
		mui('#nullData')[0].style.display = 'block';
		mui('#nullTip')[0].innerHTML = msg
	}
	function hideNullTip(){
		mui('#nullData')[0].style.display = 'none';
	}
	var exportObj = {}
	exportObj.refreshZuHePage = function(){
		getGroupList();
	};
	return exportObj
})(mui);

function refreshPageData(){
	zuhePage.refreshZuHePage()
}